package com.shangjinrong.controller.admin;

import com.shangjinrong.domain.IntegralGrade;
import com.shangjinrong.excption.Assert;
import com.shangjinrong.result.R;
import com.shangjinrong.result.ResponseEnum;
import com.shangjinrong.service.IntegralGradeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Naruto
 * <p>
 * 积分等级表Controller
 */

@RestController
@CrossOrigin
@RequestMapping("admin/integralGrade")
@Slf4j
public class AdminIntegralGradeController {

    @Autowired
    private IntegralGradeService integralGradeService;


    @GetMapping("list")
    public R listAll() {

        List<IntegralGrade> integralGradeList = integralGradeService.list();

        return R.ok().data("list", integralGradeList).message("获取列表成功");
    }

    @DeleteMapping("remove/{id}")
    public R removeById(@PathVariable Long id) {
        boolean result = integralGradeService.removeById(id);

        if (result) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("删除失败");
        }
    }


    @PostMapping("save")
    public R save(@RequestBody IntegralGrade integralGrade) {
        boolean result = integralGradeService.save(integralGrade);


        /*如果借款额度为空，则抛出异常*/
       /* if (integralGrade.getBorrowAmount() == null) {
            throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        }*/
        Assert.notNull(integralGrade.getBorrowAmount(), ResponseEnum.BORROW_AMOUNT_NULL_ERROR);

        if (result) {
            return R.ok().message("保存成功");
        } else {
            return R.error().message("保存失败");
        }
    }

    @GetMapping("get/{id}")
    public R getById(@PathVariable Long id) {
        IntegralGrade integralGrade = integralGradeService.getById(id);

        if (integralGrade != null) {
            return R.ok().data("record", integralGrade);
        } else {
            return R.error().message("数据获取失败");
        }
    }


    @PutMapping("update")
    public R updateById(@RequestBody IntegralGrade integralGrade) {
        boolean result = integralGradeService.updateById(integralGrade);

        if (result) {
            return R.ok().message("修改成功");
        } else {
            return R.error().message("修改失败");
        }
    }
}
